package com.jjshop.dialog;

import android.app.Dialog;
import android.os.Bundle;
import android.support.v7.app.AppCompatDialogFragment;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.jjshop.R;

/**
 * Created by Administrator on 2018/5/6.
 */

public class JJmailImageSaveDialog extends AppCompatDialogFragment {


    public interface OnSaveImgListener{
        void save();
    }

    public JJmailImageSaveDialog() {
    }

    private OnSaveImgListener onSaveImgListener;
    public void setOnSaveImgListener(OnSaveImgListener onSaveImgListener){
        this.onSaveImgListener = onSaveImgListener;
    }
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        Bundle bundle = getArguments();
        String img = "";
        if (bundle !=null){
            img = bundle.getString("img");
        }
//        return super.onCreateDialog(savedInstanceState);
        Dialog dialog = new Dialog(getContext(), R.style.bottomDialog);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        dialog.setContentView(R.layout.dialog_jjmail_save_img);
        dialog.setCanceledOnTouchOutside(true);
        dialog.setCancelable(true);
        setCancelable(true);

        ImageView mIvImageShare = (ImageView) dialog.findViewById(R.id.iv_share);
        ImageView mIvImageClose = (ImageView) dialog.findViewById(R.id.iv_close);
        TextView mTvLeft = (TextView) dialog.findViewById(R.id.tv_left);
        LinearLayout mLLLeft = (LinearLayout) dialog.findViewById(R.id.ll_left);

        Glide.with(getContext()).load(img).into(mIvImageShare);

        mIvImageClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        mLLLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onSaveImgListener != null){
                    onSaveImgListener.save();
                }
                dismiss();
            }
        });

// 设置宽度为屏宽, 靠近屏幕底部。
        Window window = dialog.getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
//        lp.dimAmount = 0;
        lp.gravity = Gravity.CENTER;
        lp.width = WindowManager.LayoutParams.MATCH_PARENT; // 宽度持平
        window.setAttributes(lp);

        return dialog;
    }
}
